package leetcode_top;
import java.util.*;

import org.junit.Test;
public class Ex202 {
    class Solution {
        public boolean isHappy(int n) {
            Set<Integer> set = new HashSet<>();
            while (!set.contains(n)) {
                set.add(n);
                n = sum(getAllDigits(n));
            }
            return n == 1;
        }

        public List<Integer> getAllDigits(int n) {
            List<Integer> res = new ArrayList<>();
            while (n != 0) {
                res.add(n % 10);
                n /= 10;
            }
            return res;
        }

        public int sum(List<Integer> list) {
            int sum = 0;
            for (int i = 0; i < list.size(); i++) {
                int t = list.get(i);
                sum += t * t;
            }
            return sum;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.isHappy(7));
        System.out.println(s.isHappy(1));
    }

    /* 
        select e.date, (
            select count(*)
            from email as e2
            where e2.type = 'completed'
            group by e2.date
        ) / (
            select count(*)
            from email as e2
            group by e2.date
        ) as p
        from email as e, (
            select p2.id
            from person as p2
            where p2.is_blacklist = 0
        ) as pe
        where e.send_id in pe.id and e.receive_id in pe.id;
    */
}
